package com.tiyo.system.service.impl;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiyo.common.utils.StringUtils;
import com.tiyo.common.utils.TreeUtils;
import com.tiyo.framework.shiro.util.ShiroUtils;
import com.tiyo.system.mapper.SysMenuMapper;
import com.tiyo.system.mapper.SysRoleMenuMapper;
import com.tiyo.system.mapper.domain.SysMenu;
import com.tiyo.system.mapper.domain.SysRoleMenu;
import com.tiyo.system.mapper.domain.SysUser;
import com.tiyo.system.mapper.domain.SysUserRole;
import com.tiyo.system.service.SysMenuService;
import com.tiyo.system.service.SysUserService;
@Service
public class SysMenuServiceImpl implements SysMenuService {
	public static final String PREMISSION_STRING = "perms[\"{0}\"]";
	@Autowired
	SysMenuMapper sysMenuMapper;
	@Autowired
	SysRoleMenuMapper sysRoleMenuMapper;
	@Autowired
	SysUserService sysUserService;
	@Override
	public Set<String> selectPermsByUserId(String userId) {
		Set<String> permsSet = new HashSet<>();
		List<SysUserRole> userRoles= sysUserService.selectUserRoleByUserId(userId);
		if(userRoles.size()<1) {
			return permsSet;
		}
		QueryWrapper<SysRoleMenu> roleMenuWrapper = new QueryWrapper<SysRoleMenu>();
		roleMenuWrapper.select("menu_id").in("role_id", userRoles.stream().map(userRole->userRole.getRoleId()).collect(Collectors.toList()));
		List<SysRoleMenu> roleMenus= sysRoleMenuMapper.selectList(roleMenuWrapper);
		if(roleMenus.size()<1) {
			return permsSet;
		}
		QueryWrapper<SysMenu> menuQueryWrapper = new QueryWrapper<SysMenu>();
		menuQueryWrapper.eq("visible", "1");
		menuQueryWrapper.select("perms");
		menuQueryWrapper.in("menu_id", roleMenus.stream().map(roleMenu->roleMenu.getMenuId()).collect(Collectors.toList()));
		List<SysMenu> menus= sysMenuMapper.selectList(menuQueryWrapper);
        for (SysMenu perm : menus)
        {
            if (StringUtils.isNotNull(perm))
            {
                permsSet.addAll(Arrays.asList(perm.getPerms().split(",")));
            }
        }
		return permsSet;
	}

	@Override
	public List<SysMenu> selectMenusByUser(SysUser user) {
		QueryWrapper<SysMenu> menuQueryWrapper = new QueryWrapper<SysMenu>();
		menuQueryWrapper.eq("visible", "1");
		menuQueryWrapper.in("menu_type", "M", "C");
		if(user.isAdmin()) {
			
		} else {
			List<SysMenu> sysMenus= new ArrayList<SysMenu>();
			List<SysUserRole> userRoles= sysUserService.selectUserRoleByUserId(user.getUserId());
			if(userRoles.size()<1) {
				return sysMenus;
			}
			QueryWrapper<SysRoleMenu> roleMenuWrapper = new QueryWrapper<SysRoleMenu>();
			roleMenuWrapper.select("menu_id").in("role_id", userRoles.stream().map(userRole->userRole.getRoleId()).collect(Collectors.toList()));
			List<SysRoleMenu> roleMenus= sysRoleMenuMapper.selectList(roleMenuWrapper);
			if(roleMenus.size()<1) {
				return sysMenus;
			}
			menuQueryWrapper.in("menu_id", roleMenus.stream().map(roleMenu->roleMenu.getMenuId()).collect(Collectors.toList()));
		}
		menuQueryWrapper.orderByAsc("order_num");
		List<SysMenu> list=sysMenuMapper.selectList(menuQueryWrapper);
		return TreeUtils.getChildPerms(list, "0");
	}
	
	@Override
	public List<SysMenu> selectMenuList(SysMenu menu) {
		QueryWrapper<SysMenu> menuQueryWrapper = new QueryWrapper<SysMenu>();
		SysUser user = ShiroUtils.getSysUser();
		if(user.isAdmin()) {
			
		} else {
			List<SysMenu> sysMenus= new ArrayList<SysMenu>();
			List<SysUserRole> userRoles= sysUserService.selectUserRoleByUserId(user.getUserId());
			if(userRoles.size()<1) {
				return sysMenus;
			}
			QueryWrapper<SysRoleMenu> roleMenuWrapper = new QueryWrapper<SysRoleMenu>();
			roleMenuWrapper.select("menu_id").in("role_id", userRoles.stream().map(userRole->userRole.getRoleId()).collect(Collectors.toList()));
			List<SysRoleMenu> roleMenus= sysRoleMenuMapper.selectList(roleMenuWrapper);
			if(roleMenus.size()<1) {
				return sysMenus;
			}
			menuQueryWrapper.in("menu_id", roleMenus.stream().map(roleMenu->roleMenu.getMenuId()).collect(Collectors.toList()));
		}
		menuQueryWrapper.orderByAsc("order_num");
		List<SysMenu> list=sysMenuMapper.selectList(menuQueryWrapper);
		return list;
	}

	/**
     * 查询系统所有权限
     * 
     * @return 权限列表
     */
	@Override
	public LinkedHashMap<String, String> selectPermsAll() {
		LinkedHashMap<String, String> section = new LinkedHashMap<>();
		List<SysMenu> permissions=selectMenuAll();
		if (StringUtils.isNotEmpty(permissions)){
            for (SysMenu menu : permissions) {
                section.put(menu.getUrl(), MessageFormat.format(PREMISSION_STRING, menu.getPerms()));
            }
        }
		return section;
	}

	@Override
	public List<SysMenu> selectMenuAll() {
		SysUser user = ShiroUtils.getSysUser();
		QueryWrapper<SysMenu> menuQueryWrapper = new QueryWrapper<SysMenu>();
		menuQueryWrapper.eq("visible", "1");
		if(user.isAdmin()) {
			
		} else {
			List<SysMenu> sysMenus= new ArrayList<SysMenu>();
			List<SysUserRole> userRoles= sysUserService.selectUserRoleByUserId(user.getUserId());
			if(userRoles.size()<1) {
				return sysMenus;
			}
			QueryWrapper<SysRoleMenu> roleMenuWrapper = new QueryWrapper<SysRoleMenu>();
			roleMenuWrapper.select("menu_id").in("role_id", userRoles.stream().map(userRole->userRole.getRoleId()).collect(Collectors.toList()));
			List<SysRoleMenu> roleMenus= sysRoleMenuMapper.selectList(roleMenuWrapper);
			if(roleMenus.size()<1) {
				return sysMenus;
			}
			menuQueryWrapper.in("menu_id", roleMenus.stream().map(roleMenu->roleMenu.getMenuId()).collect(Collectors.toList()));
		}
		menuQueryWrapper.orderByAsc("order_num");
		return sysMenuMapper.selectList(menuQueryWrapper);
	}

	
}
